package com.wql.rabbitmq.study;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.wql.rabbitmq.ChannelFactory;
import com.wql.rabbitmq.Constant;

public class RoutingTestForConsumer {

	public static void main(String[] args) {
		for (int i = 0; i < Constant.ROUTING_ROUTE.length; i++) {
			final int index = i;
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						consumer(index);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}).start();
		}

	}

	private static void consumer(int index) throws Exception {
		Channel channel = ChannelFactory.getChannel();
		channel.exchangeDeclare(Constant.ROUTING_NAME, "direct");
		String queueName = channel.queueDeclare().getQueue();
		channel.queueBind(queueName, Constant.ROUTING_NAME, Constant.ROUTING_ROUTE[index]);
		QueueingConsumer consumer = new QueueingConsumer(channel);
		channel.basicConsume(queueName, true, consumer);
		while (true) {
			QueueingConsumer.Delivery delivery = consumer.nextDelivery();
			String msg = new String(delivery.getBody());
			System.out.println(Constant.ROUTING_ROUTE[index] + " get msg " + msg);

		}
	}

}
